<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_AddNewMenuAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$docId = $this->getRequestParameter("docId");
		$docLang = $this->getRequestParameter("docLang");
		$formValues = $this->getFormValues("form");

		$parentDoc = $this->getDocumentProvider()->getByUniqueId($docId, $docLang);
		
		$errors = array();
		
		if($parentDoc->getDocumentModel() == "modules_website/website")
		{
			$website = $parentDoc;
		}
		else
		{
			$websites = f_relation_Manager::getInstance()->getParentsByDocType($parentDoc->getUniqueId(), "modules_website/website", $docLang);
			$website = $websites[0];
		}
		
		$menu = m_website_documents_Menu::createNew()->setLang($docLang);
		
		if(count($formValues) >= 1)
		{
			$errors["status"] = "false";
			if($formValues["label"] != "")
			{
				$menu	->setVisibleInMenu(true)
                    	->setVisibleInSiteMap(true)
						->setMenuLabel($formValues["label"])
						->setMenuHoverLabel($formValues["label"])
						->setAuthor($this->getUser())
						->setLabel($formValues["label"])
						->setWebsiteId($website->getUniqueId())
						->save(false);
				$parentDoc->addMenus($menu)->save(false);
				$errors["status"] = "success";
			}
			else
			{
				$errors["label"] = $this->getLocale('website.errors.required');
			}
		}
		
		$this->addToModel("href", $this->getRequestParameter("href"));
		$this->addToModel("errors", $errors);
		$this->addToModel("menu", $menu);
		$this->setView("success");
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "website_Add"));
	}
}